#include<bits/stdc++.h>
#define int long long 
using namespace std;
int get(int x,int y) {
	return max(1ll,min(min(x%y,y-x%y),min(y%x,x-y%x)));
}
void solve() {
	int a,b; cin>>a>>b;
	if(a*b == 0||abs(a-b) == 0)return cout<<"0\n",void();
//	if(a*b > 0)return cout<<min(abs(a)%abs(b),abs(b)%abs(a))<<"\n",void();
	if(a*b > 0) {
//		a = abs(a);	b = abs(b);
//		while()
//		int c = abs(a-b);
//		int minMod = min({get(a,b),get(b,c),get(a,c)});
//		return cout<<min({a%minMod,b%minMod,c%minMod})<<"\n",void();
		return cout<<"0\n",void();
	}
	if(a*b < 0)return cout<<min({abs(a),abs(b),abs(a+b)})<<"\n",void();
}
signed main()
{
	freopen("sub.in","r",stdin);
	freopen("sub.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t; cin>>t;
	while(t --)solve();
	return 0;
 } 
